<template>
	<view class="container">
		<image mode="widthFix" src="../../static/images/start.jpg"></image>
	</view>
</template>

<script>
	import { http } from '../../utils'
	
	export default {
		data() {
			return {
				t: 0,
				scene: ''
			}
		},
		
		onLoad(options) {
			if (options.t) {
				this.t = options.t
			}
			if (options.scene) {
				this.scene = decodeURIComponent(options.scene)
				uni.setStorageSync('scene', this.scene)
			}
			this.isLogin()
		},
		
		methods: {
			
			isLogin() {
				const that = this
				http('/no-login/islogin', {}, {hideLoading: true}).then(res => {
					if (res.code == 20000) {
						let userInfo = res.data.userInfo
						uni.setStorageSync('userInfo', userInfo)
						if (that.t > 0) {
							uni.redirectTo({
								url: '/pages/select_label/select_label?t=' + that.t
							})
						} else {
							if (userInfo.userType == 1) {
								uni.redirectTo({
									url: '/pages/index/index'
								})
							} else {
								uni.redirectTo({
									url: '/pages/teacher_group_chat_list/teacher_group_chat_list'
								})
							}
						}
					} else {
						that.doLogin()
					}
				})
			},
			
			doLogin() {
				const that = this
				
				uni.getSetting({
					success(settingRes) {
						
						if (settingRes.authSetting['scope.userInfo']) {
							
							uni.getUserInfo({
								success(userInfoRes) {
									
									uni.login({
										provider: 'weixin',
										success(loginRes) {
											
											let params = {
												code: loginRes.code,
												encodeData: userInfoRes.encryptedData,
												iv: userInfoRes.iv
											}
											http('/no-login/mini', params, {hideLoading: true}).then(res => {
												
												if (res.code == 20000) {
													let userInfo = res.data.userInfo
													uni.setStorageSync('userInfo', userInfo)
													if (that.t > 0) {
														uni.redirectTo({
															url: '/pages/select_label/select_label?t=' + that.t
														})
													} else {
														if (userInfo.userType == 1) {
															uni.redirectTo({
																url: '/pages/index/index'
															})
														} else {
															uni.redirectTo({
																url: '/pages/teacher_group_chat_list/teacher_group_chat_list'
															})
														}
													}
												}
												
											})
											
										}
									})
									
								},
								fail() {
									uni.redirectTo({
										url: '/pages/auth/auth'
									})
								}
							})
							
						} else {
							uni.redirectTo({
								url: '/pages/auth/auth'
							})
						}
						
					}
				})
				
			}
			
		}
	}
</script>

<style>
	.container {
		width: 750rpx;
	}
	
	.container image {
		width: 750rpx;
	}
</style>
